import { parse } from "querystring";

const reg =
  /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;

export const isUrl = (path: string): boolean => reg.test(path);

export const getPageQuery = () => parse(window.location.href.split("?")[1]);

// 判断是否是调试环境
export const isDEV = process.env.envType === "dev";

export const getWsPreUrl = (url = "/AutoFactory-web/websocket/") => {
  if (isDEV) {
    // 调度服务 10.54.48.42:31527/
    // return `ws://obp-test.supcon5t.com/AutoFactory-web/websocket`;
    return `ws://10.54.48.42:31527/AutoFactory-web/websocket`;
  }
  // 发布
  const protocol: string = window.location.protocol;
  if (protocol === "http:") {
    return `ws://${window.origin.replace("http://", "")}/${url}`;
  }
  return `wss://${window.origin.replace("https://", "")}/${url}`;
};
